# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
import time
from com.zzh.utils.pwdUtils import HashPwd
from com.zzh.utils.table2JSON import Table2Json

local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


class UserDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()

    # 判断手机号是否注册
    def is_regist(self, phone):
        cur = self.conn.cursor()

        sql = "select * from zzh_user WHERE login_phone=%s"
        rs = cur.execute(sql, [phone])
        cur.close()
        self.conn.close()

        if rs:
            return 1  # 返回1标志改手机号已经注册
        else:
            return 0  # 表示手机号未被注册

    '''用户注册'''

    def regist_user(self, user_info):
        login_phone = user_info['login_phone']
        login_name = login_phone
        login_pwd = HashPwd.hashPwd(user_info['login_pwd'])
        login_status = 1

        login_lasttime = local_time
        login_starttime = local_time
        login_stoptime = local_time
        user_createtime = local_time
        user_type = '0'

        cur = self.conn.cursor()
        sql = "insert into zzh_user (login_name,login_phone," \
              "login_pwd,login_status,login_lasttime,login_starttime,login_stoptime,user_type,user_createtime) " \
              "VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
        result = cur.execute(sql,
                             [login_name, login_phone, login_pwd, login_status
                                 , login_lasttime, login_starttime, login_stoptime, user_type, user_createtime])
        self.conn.commit()
        if result:
            sql2 = "select max(user_id) from zzh_user"
            rs = cur.execute(sql2)
            idn = cur.fetchall()
            newId = idn[0][0]
            cur.close()
            self.conn.close()
            return newId
        else:
            cur.close()
            self.conn.close()
            return 0

    '''更新用户信息'''

    def update_user(self, user_info, user_id):
        user_type = user_info['user_type']
        login_name = user_info['login_name']
        login_phone = user_info['login_phone']
        login_email = user_info['login_email']
        login_nickname = user_info['login_nickname']
        cur = self.conn.cursor()

        sql = "update zzh_user set user_type=%s,login_name=%s,login_phone=%s,login_email=%s,login_nickname=%s " \
              "where user_id=%s"
        result = cur.execute(sql,
                             [user_type, login_name, login_phone, login_email, login_nickname, user_id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return result

    '''用户登录验证'''

    def user_login(self, loginPhone, password):
        cur = self.conn.cursor()
        sql = "select login_pwd from zzh_user where login_phone=%s"
        result = cur.execute(sql, [loginPhone])
        if not result:
            return 0, None, None  # 0-用户不存在
        else:
            login_reult = cur.fetchall()
            hashPwd = login_reult[0][0]

            if (HashPwd.verifyPwd(password, hashPwd)):
                updatesql = "update zzh_user set login_starttime=%s WHERE login_phone=%s"
                cur.execute(updatesql, [local_time, loginPhone])
                self.conn.commit()
                sql = "select user_id, user_type from zzh_user WHERE login_phone=%s"
                rs = cur.execute(sql, [loginPhone])
                user_list = cur.fetchall()
                user_id = user_list[0][0]
                user_type = user_list[0][1]

                cur.close()
                self.conn.close()
                return 1, user_id, user_type  # 登陆成功
            else:
                return -1, None, None  # 密码不正确，登陆失败

    '''重置密码'''

    def reset_passwd(self, new_pwd, phone):
        cur = self.conn.cursor()
        hashPwd = HashPwd.hashPwd(new_pwd)
        sql = "update zzh_user set login_pwd=%s where login_phone=%s"
        result = cur.execute(sql, [hashPwd, phone])
        self.conn.commit()

        cur.close()
        self.conn.close()
        return result

    def update_passwd(self, user_id, oldPwd, newPwd):
        """修改密码"""
        cur = self.conn.cursor()
        sql = "select login_pwd from zzh_user WHERE user_id=%s"
        rs = cur.execute(sql, [user_id])
        result = cur.fetchall()
        hashPass = result[0][0]
        if (HashPwd.verifyPwd(oldPwd, hashPass)):
            hashNewPwd = HashPwd.hashPwd(newPwd)
            sql = "update zzh_user set login_pwd=%s where user_id=%s"
            rs = cur.execute(sql, [hashNewPwd, user_id])
            self.conn.commit()
            cur.close()
            self.conn.close()
            if rs:
                return 1
            else:
                return 0
        else:
            return -1

    '''根据用户id获取用户信息'''

    def get_user_info(self, user_id):
        cur = self.conn.cursor()

        sql = "select user_id,user_type,login_name,login_phone,login_email,login_nickname from zzh_user WHERE user_id=%s"

        rs = cur.execute(sql, [user_id])
        uslist = cur.fetchall()
        user_type = uslist[0][1]
        rs2json = Table2Json.user_list_to_json(uslist)

        return rs2json, user_type

    def get_user_by_id(self,user_id):
        cur = self.conn.cursor()

        sql = "select user_id,user_type,login_name,login_phone,login_email,login_nickname from zzh_user WHERE user_id=%s"

        rs = cur.execute(sql, [user_id])
        uslist = cur.fetchone()

        return uslist

